unit UEditSign;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,
// selfmade units
  UConfig, USigns, uUtils;

type
  TfrmEditSign = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    procedure BtnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    CurSignId: Integer;
    SignControls: array[NSignParam] of THackControl;
  public
    function Add(): TModalResult;
    function Edit(Index: Integer): TModalResult;
  end;

var
  frmEditSign: TfrmEditSign;

implementation

{$R *.dfm}

function TFrmEditSign.Add(): TModalResult;
begin
  Result := Edit(SignConfig.Add);
end;

procedure TFrmEditSign.BtnOKClick(Sender: TObject);
var
  i: NSignParam;
begin
  for i := Low(NSignParam) to High(NSignParam) do
    if SignParamType[i] = npBoolean then
      SignConfig.Items[CurSignId, i] := TBoolCtrl(SignControls[i]).Text
    else
      SignConfig.Items[CurSignId, i] := SignControls[i].Text;
end;


function TFrmEditSign.Edit(Index: Integer): TModalResult;
var
  i: NSignParam;
begin
  CurSignId := Index;
  for i := Low(NSignParam) to High(NSignParam) do
    if SignParamType[i] = npBoolean then
      TBoolCtrl(SignControls[i]).Text := SignConfig.Items[Index, i]
    else
      SignControls[i].text := SignConfig.Items[Index, i];
  Result := ShowModal;
end;

procedure TFrmEditSign.FormCreate(Sender: TObject);
var
  i: NSignParam;
begin
  for i := Low(NSignParam) to High(NSignParam) do
  begin
    with TLabel.Create(Self) do
    begin
      Top := Ord(i) * 30;
      Parent := Self;
      Caption := SignParamNames[i];
    end;
    TControl(SignControls[i]) := ControlTypes[SignParamType[i]].Create(Self);
    with SignControls[i] do
    begin
      Top := Ord(i) * 30;
      Left := 100;
      Tag := Ord(i);
      Parent := Self;
      if i in [spID] then
        Enabled := False;
    end;
  end;
end;


end.
